/*-
 * Fujitsu-Siemens ACPI Wireless Lan Switch Module
 * This module is designed to work with Fujitsu-Siemens Amilo 1650G Laptop
 * 
 * Copyright (c)2008  EBASoft Artur Bac <arturATebasoft.com.pl>
 * All Rights Reserved.
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD: src/sys/dev/acpi_support/acpi_fujsie.c,v 1.00 2008/01/26 03:45:24 abac Exp $");

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>

#include <contrib/dev/acpica/acpi.h>
#include <dev/acpica/acpivar.h>

#include <sys/sysctl.h>
				 
#include "acpi_if.h"
#include "opt_acpi.h"

#define MY_MODULE_NAME 				"Fujitsu-Siemens-Amilo"
#define HKEYSYS_WIRELESS_MASK 		0x35
#define HKEYSYS_DIG_MAGIC 			0x9610

//FAQ about WMI and ACPI for Windows Instrumentation
//How does WMI find ACPI/ASL code?
//In ASL, the developer creates a device with an _HID of PNP0c14. 
//The operating system enumerates the device and loads the Wmiacpi.sys driver on top of it.
//Device (AMW0)
//{
//  Name (_HID, "pnp0c14")
#define AMW0_NAMESPACE 			"pnp0c14" 

//Method Execution	
//Object name : WMxx	
//Parameters : ULONG, method ID, buffer	
//Control method required : Yes. Required for data blocks specified in the data block
//GUID mapping array and that have the WMIACPI_REG_METHOD flag set.
//\_SB.LPC0.AMW0

#define AMW_METHOD_WMAB 		"WMAB"

#define METHOD_WIFI_ENABLE 		0
#define METHOD_WIFI_LATER		1

//Module specific static data
struct acpi_fujsie_softc {
	device_t					dev;
	int							antena_enabled;
	struct sysctl_ctx_list		sysctl_ctx;
	struct sysctl_oid			*sysctl_tree;
};

//This is a buffer of arguments send to WMI thru ACPI
typedef struct wmab_args_in_t {
	UINT32 eax;
	UINT32 ebx;
	UINT32 ecx;
	UINT32 edx;
} WMAB_ARGS_IN;

static int 		acpi_fujsie_probe(device_t dev);
static int 		acpi_fujsie_attach(device_t dev);
static int 		acpi_fujsie_detach(device_t dev);
static int 		acpi_fujsie_suspend(device_t dev);
static int 		acpi_fujsie_resume(device_t dev);
static int		acpi_fujsie_shutdown(device_t dev);

static ACPI_STATUS acpi_fujsie_switch_wifi(device_t dev, uint8_t enable );
//static ACPI_STATUS acpi_fujsie_read_wifi( device_t dev, uint32_t *result );

static int	acpi_fujsie_sysctl(SYSCTL_HANDLER_ARGS);

static device_method_t acpi_fujsie_methods[] = {
	DEVMETHOD(device_probe, acpi_fujsie_probe),
	DEVMETHOD(device_attach, acpi_fujsie_attach),
	DEVMETHOD(device_detach, acpi_fujsie_detach),
	DEVMETHOD(device_suspend, acpi_fujsie_suspend),
	DEVMETHOD(device_resume, acpi_fujsie_resume),
	DEVMETHOD(device_shutdown,	acpi_fujsie_shutdown),
	{0, 0}
};

static driver_t	acpi_fujsie_driver = {
	"acpi_fujitsu_siemens_amilo",
	acpi_fujsie_methods,
	sizeof(struct acpi_fujsie_softc),
};

static devclass_t acpi_fujsie_devclass;

ACPI_MODULE_NAME( MY_MODULE_NAME );
DRIVER_MODULE(acpi_fujitsu_siemens_amilo, acpi, acpi_fujsie_driver, acpi_fujsie_devclass, 0, 0);
MODULE_DEPEND(acpi_fujitsu_siemens_amilo, acpi, 1, 1, 1);
MODULE_VERSION(acpi_fujitsu_siemens_amilo, 1);

ACPI_SERIAL_DECL(fujsie, "Fujitsu-Siemens Amilo A1650G ACPI");


static int
acpi_fujsie_probe(device_t dev)
{
	static char * acpi_fujitsu_siemens_amilo_ids[] = { AMW0_NAMESPACE, NULL};

	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);

	char *str = ACPI_ID_PROBE(device_get_parent(dev), dev, acpi_fujitsu_siemens_amilo_ids);
	if ( 	acpi_disabled(MY_MODULE_NAME) || str  == NULL || device_get_unit(dev) != 0 ) 
		return (ENXIO);
	
	device_set_desc(dev, "Fujitsu-Siemens ACPI Wireless Lan Switch Module atached to device AMW0");
	return 0;
}

static int
acpi_fujsie_attach(device_t dev)
{
	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);

	struct acpi_fujsie_softc *sc = device_get_softc(dev);
	sc->antena_enabled = 0;
	
	struct acpi_softc * acpi_sc = acpi_device_get_parent_softc(dev);
	sc->dev = dev;

	ACPI_SERIAL_BEGIN(fujsie);
	//Initializing sysctl tree
	sysctl_ctx_init(&sc->sysctl_ctx);

	//joining hw.acpi node
	sc->sysctl_tree = SYSCTL_ADD_NODE( &sc->sysctl_ctx, SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
	    								OID_AUTO, "a1650g", CTLFLAG_RD, 0, "Amilo A1650G extras");	

	//adding callbacks for our members
	SYSCTL_ADD_PROC( &sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
		    			"antena", CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_ANYBODY,
		    			sc, 0, acpi_fujsie_sysctl, "I", "Wifi antena status");


	ACPI_SERIAL_END( fujsie) ;

	
	acpi_fujsie_switch_wifi(dev, sc->antena_enabled);

	//uint32_t result;
	//acpi_fujsie_read_wifi( dev, &result );
	//device_printf( dev, "Read status EC %d\n", result );

	return 0;
}

static int
acpi_fujsie_detach(device_t dev)
{
	return acpi_fujsie_shutdown( dev );
}
static int
acpi_fujsie_shutdown( device_t dev )
{
	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
	struct acpi_fujsie_softc *sc = device_get_softc(dev);

	ACPI_SERIAL_BEGIN(fujsie);
	//in reality they are allocated and freed based on their reference count
	sysctl_ctx_free( &sc->sysctl_ctx );

	ACPI_SERIAL_END( fujsie );
	acpi_fujsie_switch_wifi(dev, 0);
	return 0;
}
static int
acpi_fujsie_suspend(device_t dev)
{
	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
	acpi_fujsie_switch_wifi(dev, 0);
	return 0;
}

static int
acpi_fujsie_resume(device_t dev)
{
	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
	struct acpi_fujsie_softc *sc = device_get_softc(dev);
	if ( sc->antena_enabled != 0 )
		acpi_fujsie_switch_wifi( dev, 1 );
	return (0);
}

//define SYSCTL_HANDLER_ARGS struct sysctl_oid *oidp, void *arg1, int arg2, 
//	struct sysctl_req *req
static int
acpi_fujsie_sysctl( SYSCTL_HANDLER_ARGS )
{
	int				arg = 0 ;
	int				function_num;
	int 			error = 0;

	struct acpi_fujsie_softc *sc = (struct acpi_fujsie_softc *)oidp->oid_arg1;
	arg = sc->antena_enabled;
	
	function_num = oidp->oid_arg2;
	//method = sysctl_table[function_num].method;

	ACPI_SERIAL_BEGIN(fujsie);
	/* Get the current value */
	error = sysctl_handle_int(oidp, &arg, 0, req);
	ACPI_SERIAL_END( fujsie );

	if (error != 0 || req->newptr == NULL) {
		//device_printf(sc->dev, "sysctl_handle_int error\n");
		goto out;
	}
	sc->antena_enabled = (arg == 1 ? 1 : 0) ;
	//device_printf(sc->dev, "sysctl recived :[%d]\n", arg );
	/* Update the value */
	acpi_fujsie_switch_wifi(sc->dev, sc->antena_enabled);

out:
	return (error);
}


//Future todo, if it is possible
/*static ACPI_STATUS
acpi_fujsie_read_wifi( device_t dev, uint32_t *result ) {

	ACPI_INTEGER value;
	ACPI_STATUS status = ACPI_EC_READ( dev, 0x0A, &value, 1);
	*result = (uint32_t)value;

	return status;
}*/

static ACPI_STATUS
acpi_fujsie_switch_wifi( device_t dev, uint8_t enable )
{
	WMAB_ARGS_IN wmi_data = {
		HKEYSYS_DIG_MAGIC,
  		(( enable ? (1<<8) : 0 ) | HKEYSYS_WIRELESS_MASK),
		0,
  		0
	};
	ACPI_BUFFER wmi_result;

	ACPI_OBJECT wmi_args[3] ;
	ACPI_OBJECT_LIST wmi_args_list;

	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
	
	//Arguments for WMAB
	wmi_args[0].Type = ACPI_TYPE_INTEGER;
	wmi_args[0].Integer.Value = 1;
	wmi_args[1].Type = ACPI_TYPE_INTEGER;
	wmi_args[1].Integer.Value = 1;

	wmi_args[2].Type = ACPI_TYPE_BUFFER;
	wmi_args[2].Buffer.Length =  sizeof( WMAB_ARGS_IN );
	wmi_args[2].Buffer.Pointer = (UINT8 *) &wmi_data;
	
	//Prepare list
	wmi_args_list.Count = 3;
	wmi_args_list.Pointer = wmi_args;

	//Allow allcate outgoing buffer when it will be necessery
	wmi_result.Pointer = NULL;
	wmi_result.Length = ACPI_ALLOCATE_BUFFER;

	//Lets roll
	ACPI_SERIAL_BEGIN(fujsie);
	//ACPI_STATUS status = AcpiEvaluateObject( sc->handle, AMW_METHOD_WMAB, &wmi_args_list, &wmi_result );
	ACPI_STATUS status = ACPI_EVALUATE_OBJECT( device_get_parent(dev), dev, AMW_METHOD_WMAB, &wmi_args_list, &wmi_result );
	ACPI_SERIAL_END(fujsie);

	AcpiOsFree( wmi_result.Pointer );

	return status;
}

